package very.util.mark
import better.files._
import org.json4s.Formats
import org.json4s.native.JsonMethods.parse
import org.json4s.native.Serialization.write

class FileMark[T](filePath: String)(implicit formats: Formats, mf: scala.reflect.Manifest[T]) {
  val file = File(filePath).createIfNotExists(false, true)

  def getMark: Option[T] = {
    val data = file.lines.mkString("\n")
    if (data.isEmpty) {
      None
    } else {
      Some(parse(data).extract[T])
    }
  }

  def writeMark(t: T): Unit = {
    val data = write(t)
    file.writeText(data)
  }
}
